import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import Login from "@/pages/Login";
import Home from "@/pages/Home";
import Goodissus from "@/pages/Home/Goodissus";
import Usermanage from "@/pages/Home/Usermanage";
import Usermessage from "@/pages/Home/Usermessage";

//#region 解决路由的多次跳转
// 先把VueRouter原型对象的push，先保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

// 重写push|replace
// 第一个参数：告诉原来push方法，你往哪里跳转（传递哪些参数）
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    //call||apply区别
    //相同点，都可以调用函数一次，都可以篡改函数的上下文一次
    //不同点：call与apply传递参数：call传递参数用逗号隔开，apply方法执行，传递数组
    originPush.call(this, location, resolve, reject);
  } else {
    //push方法没有产地第二个参数|第三个参数
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
//重写VueRouter.prototype身上的replace方法了
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
//#endregion

export default new VueRouter({
  routes: [
    { path: "/", redirect: "/login" },
    {
      path: "/login",
      component: Login,
      meta: { title: "登入页" },
    },
    {
      name: "home",
      path: "/home/:userInfo?",
      component: Home,
      meta: { title: "首页" },
      props: true,
      children: [
        {
          path: "goodissus",
          component: Goodissus,
          meta: { title: "物品发布" },
        },
        {
          path: "usermanage",
          component: Usermanage,
          meta: { title: "用户管理" },
        },
        {
          path: "usermessage",
          component: Usermessage,
          meta: { title: "个人信息" },
        },
      ],
      redirect: "/home/goodissus",
    },
  ],
});
